package com.ghy.nba.controllers;

import com.ghy.nba.dao.BallStarRepository;
import com.ghy.nba.entities.BallStar;
import com.ghy.nba.entities.BallTeam;
import com.ghy.nba.services.BallTeamService;
import com.ghy.nba.services.BallstarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/ballteams")
public class BallTeamControllers {

    @Autowired
    BallTeamService ballTeamService;

    @Autowired
    BallstarService ballstarService;

    @GetMapping()
    public String disBallTeams(Model model){
        List<BallTeam>ballTeams=ballTeamService.getALL();
        model.addAttribute("ballteams",ballTeams);
        return "ballteam/list-ballteam";
    }

    @GetMapping("/new")
    public String displayBallTeamFrom(Model model){
        BallTeam aballteam=new BallTeam();
        List<BallStar> ballStars= ballstarService.getALL();
        model.addAttribute("ballteam",aballteam);
        model.addAttribute("allBallstar",ballStars);
        return "ballteam/new-ballteam";
    }

    @PostMapping("/save")
    public String createBallTeam(Model model, @Valid BallTeam ballTeam, Errors errors){
            if (errors.hasErrors())
                return "ballteam/new-ballteam";

            ballTeamService.save(ballTeam);
        return "redirect:/ballteams";
    }

    @GetMapping("/update")
    public String displayBallTeamUpdateForm(@RequestParam("id")long theId, Model model){
        BallTeam theBallteam = ballTeamService.findByBallteamIdId(theId);
        model.addAttribute("ballteam",theBallteam);
        return "ballteam/new-ballteam";
    }

    @GetMapping("/delete")
    public String deleteBallteam(@RequestParam("id")long theId, Model model){

        BallTeam theBallteam = ballTeamService.findByBallteamIdId(theId);
        ballTeamService.delete(theBallteam);
        return "redirect:/ballteams";
    }
}
